package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Source;
import service.SourceService;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sources")
@Api(tags = "号源管理接口", description = "提供医生出诊号源的查询和管理功能")
public class SourceController {

    @Autowired
    private SourceService sourceService;

    @GetMapping("/all")
    @ApiOperation(value = "获取所有医生的出诊号源信息", notes = "查询系统中所有医生的出诊号源信息列表")
    public List<Source> getAllSources() {
        return sourceService.getAllSources();
    }

    @GetMapping("/date/{date}")
    @ApiOperation(value = "根据日期获取号源信息", notes = "查询指定日期的所有号源信息")
    @ApiImplicitParam(name = "date", value = "查询日期，格式：yyyy-MM-dd", required = true, dataType = "String", paramType = "path")
    public List<Source> getSourcesByDate(@PathVariable String date) {
        return sourceService.getSourcesByDate(date);
    }

    @GetMapping("/department/{departmentName}")
    @ApiOperation(value = "根据科室获取号源信息", notes = "查询指定科室的号源信息")
    @ApiImplicitParam(name = "departmentName", value = "科室名称", required = true, dataType = "String", paramType = "path")
    public List<Source> getSourcesByDepartment(@PathVariable String departmentName) {
        return sourceService.getSourcesByDepartment(departmentName);
    }

    @PostMapping("/register")
    @ApiOperation(value = "挂号操作", notes = "执行挂号操作，减少对应医生的剩余号源数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "挂号日期，格式：yyyy-MM-dd", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "morningWork", value = "上午出诊情况", required = true, dataType = "String", paramType = "query")
    })
    public int updateRemainingSources(@RequestParam String doctorId,
                                      @RequestParam String date,
                                      @RequestParam String morningWork) {
        return sourceService.updateRemainingSources(doctorId, date, morningWork);
    }

    @GetMapping("/updated/{doctorId}/{date}")
    @ApiOperation(value = "获取更新后的号源信息", notes = "查询指定医生在指定日期的最新号源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "date", value = "日期，格式：yyyy-MM-dd", required = true, dataType = "String", paramType = "path")
    })
    public Source getUpdatedSource(@PathVariable String doctorId, @PathVariable String date) {
        return sourceService.getUpdatedSource(doctorId, date);
    }

    @GetMapping("/statistics/{date}")
    @ApiOperation(value = "统计各科室的可预约号源数量", notes = "统计指定日期各科室的可预约号源数量")
    @ApiImplicitParam(name = "date", value = "统计日期，格式：yyyy-MM-dd", required = true, dataType = "String", paramType = "path")
    public List<Map<String, Object>> countSourcesByDepartment(@PathVariable String date) {
        return sourceService.countSourcesByDepartment(date);
    }

    @GetMapping("/doctor/{doctorId}")
    @ApiOperation(value = "获取指定医生的出诊时间表", notes = "查询指定医生的出诊时间安排")
    @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "String", paramType = "path")
    public List<Source> getDoctorSchedule(@PathVariable String doctorId) {
        return sourceService.getDoctorSchedule(doctorId);
    }
}